package Test.stream流测试;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
 * @Author kirito
 * @Date 2023/12/13 19:01
 * @PackageName:Test.stream流测试
 * @ClassName: a
 * @Description:
 * @Version 1.0
 */
public class StreamDemo1 {
    public static void main(String[] args) {
        ArrayList<String> names = new ArrayList<>();
        Collections.addAll(names, "张三丰", "张无忌", "张三", "李四", "李明", "张强");

        // 从集合中找到姓张，且名称长度是3的姓名。
        names.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("张");
            }
        }).filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.length()==3;
            }
        }).forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        // Lambda 化简
        names.stream().filter(s -> s.startsWith("张")).filter(s -> s.length()==3).forEach(s-> System.out.println(s));
        names.stream().filter(s -> s.startsWith("张")&&s.length()==3).forEach(System.out::println);
    }
}
